﻿using System.Drawing;

namespace MachineVision.API
{
    public class MyTools
    {
        static string PathStr = @"C:\Images";
        public static string Base64toImage(string base64)
        {
            byte[] bytes = Convert.FromBase64String(base64);
            MemoryStream memStream = new MemoryStream(bytes);
            Image mImage = Image.FromStream(memStream);

            Bitmap bp = new Bitmap(mImage);
            MemoryStream ms = new MemoryStream();
            string path = Path.Combine(PathStr, DateTime.Now.ToString("yyyyMM"), DateTime.Now.ToString("MMdd"));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\"+DateTime.Now.ToString("yyyyMMddHHssfff") + ".jpg";
            bp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
            return path;
        }

        public static string Imagetoase64(Image file)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                file.Save(stream, file.RawFormat);
                byte[] imageByte = stream.ToArray();
                return Convert.ToBase64String(imageByte);
            }
        }
    }
}
